package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.ComCompanyMapper;
import com.bf.electroplating.mapper.ComEnvironmentallyFacilityMapper;
import com.bf.electroplating.pojo.dto.facility.ComEnvironmentallyFacilityDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ComEnvironmentallyFacility;
import com.bf.electroplating.pojo.enums.EnvironmentalFacilityTypeEnum;
import com.bf.electroplating.pojo.vo.facility.ComEnvironmentallyFacilityListVO;
import com.bf.electroplating.pojo.vo.facility.ComEnvironmentallyFacilityVO;
import com.bf.electroplating.service.ComEnvironmentallyFacilityService;
import com.bf.electroplating.service.PubAttachmentService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 15451
 * @description 针对表【com_environmentally_facility(环保设施表)】的数据库操作Service实现
 * @createDate 2025-05-09 11:11:32
 */
@Service
public class ComEnvironmentallyFacilityServiceImpl extends ServiceImpl<ComEnvironmentallyFacilityMapper, ComEnvironmentallyFacility>
        implements ComEnvironmentallyFacilityService {

    @Resource
    private ComCompanyMapper companyMapper;

    @Resource
    private PubAttachmentService attachmentService;

    @Override
    public Page<ComEnvironmentallyFacilityVO> getFacilityList(ComEnvironmentallyFacilityDTO dto) {
        Page<ComEnvironmentallyFacility> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<ComEnvironmentallyFacility> wrapper = new LambdaQueryWrapper<>();

        // 查询条件：环保设施名称
        if (StringUtils.hasText(dto.getEnvironmentalFacilityName())) {
            wrapper.like(ComEnvironmentallyFacility::getEnvironmentalFacilityName, dto.getEnvironmentalFacilityName());
        }

        // 查询条件：环保设施类型
        if (StringUtils.hasText(dto.getEnvironmentalFacilityType())) {
            wrapper.like(ComEnvironmentallyFacility::getEnvironmentalFacilityType, dto.getEnvironmentalFacilityType());
        }
        if (StringUtils.hasText(dto.getCompanyName())) {
            wrapper.eq(ComEnvironmentallyFacility::getCompanyId, dto.getCompanyName());
        }

        Page<ComEnvironmentallyFacility> facilityPage = this.page(page, wrapper);
        Page<ComEnvironmentallyFacilityVO> voPage = new Page<>();
        BeanUtils.copyProperties(facilityPage, voPage, "records");
        // 获取所有涉及的企业ID
        List<String> companyIds = facilityPage.getRecords().stream()
                .map(ComEnvironmentallyFacility::getCompanyId)
                .filter(StringUtils::hasText)
                .distinct()
                .collect(Collectors.toList());

        // 批量查询企业信息
        Map<String, String> companyNameMap = new HashMap<>();
        if (!companyIds.isEmpty()) {
            LambdaQueryWrapper<ComCompany> companyWrapper = new LambdaQueryWrapper<>();
            companyWrapper.in(ComCompany::getId, companyIds);
            List<ComCompany> companies = companyMapper.selectList(companyWrapper);
            companyNameMap = companies.stream()
                    .collect(Collectors.toMap(ComCompany::getId, ComCompany::getCompanyName, (k1, k2) -> k1));
        }

        // 封装VO对象
        Map<String, String> finalCompanyNameMap = companyNameMap;
        List<ComEnvironmentallyFacilityVO> voList = facilityPage.getRecords().stream().map(facility -> {
            ComEnvironmentallyFacilityVO vo = new ComEnvironmentallyFacilityVO();
            BeanUtils.copyProperties(facility, vo);
            vo.setEnvironmentalFacilityType(EnvironmentalFacilityTypeEnum.getDesc(facility.getEnvironmentalFacilityType()));
            // 设置企业名称
            if (StringUtils.hasText(facility.getCompanyId())) {
                vo.setCompanyName(finalCompanyNameMap.getOrDefault(facility.getCompanyId(), ""));
            }
            return vo;
        }).collect(Collectors.toList());

        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public ComEnvironmentallyFacilityVO getFacilityById(String id) {
        ComEnvironmentallyFacility facility = this.getById(id);
        if (facility == null) {
            return null;
        }

        ComEnvironmentallyFacilityVO vo = new ComEnvironmentallyFacilityVO();
        BeanUtils.copyProperties(facility, vo);

        // 查询企业名称
        if (StringUtils.hasText(facility.getCompanyId())) {
            ComCompany company = companyMapper.selectById(facility.getCompanyId());
            if (company != null) {
                vo.setCompanyName(company.getCompanyName());
            }
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveFacility(ComEnvironmentallyFacilityVO vo) {
        ComEnvironmentallyFacility facility = new ComEnvironmentallyFacility();
        BeanUtils.copyProperties(vo, facility);
        return this.save(facility);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateFacility(ComEnvironmentallyFacilityVO vo) {
        ComEnvironmentallyFacility facility = new ComEnvironmentallyFacility();
        BeanUtils.copyProperties(vo, facility);
        boolean result = this.updateById(facility);

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacility(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteFacility(List<String> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public List<ComEnvironmentallyFacilityListVO> getFacilityList() {
        List<ComEnvironmentallyFacility> facilities = this.list();
        if (!facilities.isEmpty()) {
            return facilities.stream().map(facility -> {
                ComEnvironmentallyFacilityListVO vo = new ComEnvironmentallyFacilityListVO();
                vo.setId(facility.getId());
                vo.setName(facility.getEnvironmentalFacilityName());
                return vo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}




